<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Analog</em>
</p>
<p class='vspace'></p><h3>Analog Write with 7 <span class='wikiword'>LEDs</span> that are tied to the analogWrite-capable pins on the MSP-EXP430G2 LaunchPad.</h3>
<p>This example fades 7  <span class='wikiword'>LEDs</span> up and the down, one by one, on an MSP-EXP430G2 LaunchPad. 
</p>
<p class='vspace'></p><div  class='BOM' >
<h3>Hardware Required</h3>
<ul><li>MSP-EXP430G2 LaunchPad
</li><li>(7) <span class='wikiword'>LEDs</span>
</li><li>(7) 220 ohm resistors
</li><li>hook up wire
</li><li>breadboard
</li></ul></div>
<h3>Relevant Groundwork</h3>
<p class='vspace'></p><p>Connect the longer, positive legs of (anodes) 7 <span class='wikiword'>LEDs</span> to digital pins 4, 9, 10, 12, 13, 14, 19 through 220 ohm current limiting resistors. Connect the shorter, negative legs (cathodes) to ground. 
</p>
<p class='vspace'></p><h3>Circuit</h3>
<div  class='circuit' >
<p class='vspace'></p><div><img height='250px' src='../reference/img/AnalogWrite_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p></div>
<p class='vspace'></p><h3>Schematic</h3>
<div  class='circuit' >
<div><img height='400px' src='../reference/img/AnalogWrite_schem.png' alt='' title='' /></div>
</div>
<p class='vspace'></p><h3>Code Explanation</h3>
<p>In the <code>setup()</code> function of the code below, a <a class='wikilink' href='For.html'><code>for()</code></a> loop is used to assign digital pins 4, 9, 10, 12, 13, 14, 19 of the LaunchPad as outputs.
</p>
<p class='vspace'></p><p>Next, in the <code>loop()</code> function of the program below, a trio of nested <code>for()</code> loops are used. 
</p>
<p class='vspace'></p><p>The first of these loops, 
</p>
<p class='vspace'></p>
<pre>
for (int thisPin =0; analogWritePin[thisPin] <= 7; thisPin++)
</pre>
<p class='vspace'></p><p>moves through each of the LEDS one by one, from the lowest pin to the highest. Before this loop is allowed to move from one pin to the next, two things must be accomplished. First, you brighten the individual LED through these lines of code:
</p>
<p class='vspace'></p>
<pre>
for (int brightness = 0; brightness < 255; brightness++) {
      analogWrite(analogWritePin[thisPin], brightness);
      delay(2);
}
</pre>
<p class='vspace'></p><p>With each pass through the loop above, the variable brightness increases by one point, and that value is  written to the pin currently selected to the main loop. One that pin reaches the maximum PWM value (255), the following loop kicks in:
</p>
<p class='vspace'></p>
<pre>
for (int brightness = 255; brightness >= 0; brightness--) {
      analogWrite(analogWritePin[thisPin], brightness);
      delay(2);
}
</pre>
<p class='vspace'></p><p>This loop subtracts a point from the brightness variable, dimming the LED back down to 0. Once zero is reached, the main <code>for()</code> loop kicks in, and the program moves on to the next LED pin, repeating all the steps mentioned above. 
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  Using all of the analogWrite-capable pins on the LaunchPad!
    
  This sketch fades LEDs up and down one at a time on digital pins 4, 9, 10, 12, 13, 14, 19.  
  This sketch was written for the MSP-EXP430G2 LaunchPad, and leverages the analogWrite capable
  pins on that particular board.     
  The circuit:
  * LEDs attached from pins 4, 9, 10, 12, 13, 14, 19 to ground.

  created 8 Feb 2009
  by Tom Igoe
  
  Modified by Adrian Fernandez on 4/12/2013   This example code is in the public domain.
  
 */
// These constants won't change.  They're used to give names
// to the pins used:
const int analogWritePin[] = {4, 9, 10, 12, 13, 14, 19};


void setup() {
  // set pins 4, 9, 10, 12, 13, 14, 19 as outputs:
  for (int thisPin =0; thisPin <= 7; thisPin++) { 
    pinMode(analogWritePin[thisPin], OUTPUT); 
  }
}

void loop() {
  // iterate over the pins:
  for (int thisPin =0; thisPin <= 7; thisPin++) { 
    // fade the LED on thisPin from off to brightest:
    for (int brightness = 0; brightness < 255; brightness++) {
      analogWrite(analogWritePin[thisPin], brightness);
      delay(2);
    } 
    // fade the LED on thisPin from brithstest to off:
    for (int brightness = 255; brightness >= 0; brightness--) {
      analogWrite(analogWritePin[thisPin], brightness);
      delay(2);
    } 
    // pause between LEDs:
    delay(100);
  }
}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
- incorporate multiple analog signals into the program
<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='For.html'>for</a>()
</li><li><a class='wikilink' href='AnalogWrite.html'>analogWrite</a>()
</li><li><a class='wikilink' href='Delay.html'>delay</a>()
</li><li><a class='wikilink' href='Tutorial_AnalogInput.html'>AnalogInput</a> - use a potentiometer to control the blinking of an LED
</li><li><a class='wikilink' href='Tutorial_AnalogInOutSerial.html'>AnalogInOutSerial</a> - read an analog pin, map the result, and use that data to dim or brighten an LED
</li><li><a class='wikilink' href='Tutorial_Fade.html'>Fade</a> - use an analog input to fade an LED
</li><li><a class='wikilink' href='Tutorial_Calibration.html'>Calibration</a> - calibrating analog sensor readings
</li><li><a class='wikilink' href='Tutorial_ForLoop.html'>ForLoop</a> - How to use a <code>for()</code> loop (aka Knight Rider)
</li></ul><p class='vspace'></p>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
